package com.interauto.Controller.casepage;

import com.interauto.bean.web.ResPonseinfo;
import com.interauto.bean.web.RunTaskWeb;
import com.interauto.service.web.RunTaskPageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 任务相关的哈
 */
@Controller
@RequestMapping(value = "/api/task")
public class TaskApiController {
    @Autowired
    private RunTaskPageService runTaskPageService;

    @RequestMapping(value = "/addtask",method = RequestMethod.POST)
    @ResponseBody
    public ResPonseinfo addtaskinfo(@RequestBody RunTaskWeb runTaskWeb){
        String taskname=runTaskWeb.getTaskname();
        int runcycle=runTaskWeb.getRuncycle();
        String taskcases=runTaskWeb.getCaseinfo();
        String callnumber=runTaskWeb.getCallnumber();
        if(StringUtils.isEmpty(taskname) || StringUtils.isEmpty(taskcases) || StringUtils.isEmpty(callnumber)){
            return new ResPonseinfo(-1,"请求参数有空");
        }
        if(runcycle<1){
            return new ResPonseinfo(-2,"运行周期不能小于5分钟");
        }
        int taskid=runTaskWeb.getId();
        if(taskid>0){  //如果存在任务ID，则肯定是走修改
            return runTaskPageService.upruntask(runTaskWeb);
        }
        return runTaskPageService.addruntask(runTaskWeb);
    }

    @RequestMapping(value = "/gettask",method = RequestMethod.GET)
    @ResponseBody
    public RunTaskWeb getRuntakInfo(@RequestParam(value = "taskid") int taskid){
        return runTaskPageService.getRunTaskWeb(taskid);
    }

    @RequestMapping(value = "/pause",method = RequestMethod.GET)
    @ResponseBody
    public ResPonseinfo pausetaskinfo(@RequestParam(value = "taskid") int taskid,
                                      @RequestParam(value = "taskstatus") int taskstatus) {
        boolean status = runTaskPageService.goruntaskstatu(taskid, taskstatus);
        if(status){
            return new ResPonseinfo(0,"操作成功");
        }
        return new ResPonseinfo(-1,"操作异常");
    }

    @RequestMapping(value = "/del",method = RequestMethod.GET)
    @ResponseBody
    public ResPonseinfo pausetaskinfo(@RequestParam(value = "taskid") int taskid) {
        boolean status = runTaskPageService.delruntask(taskid);
        if(status){
            return new ResPonseinfo(0,"操作成功");
        }
        return new ResPonseinfo(-1,"操作异常");
    }

}
